package railRoad.web.slaves;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import railRoad.Schedule;
import railRoad.Station;
import railRoad.web.slaves.interfaces.ISlaveServlet;
import railRoad.worker.FactoryRW;

public class StationAddServlet implements ISlaveServlet {

	public void work(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {

		Map<?, ?> parameterMap = req.getParameterMap();

		if (parameterMap.containsKey("name")
				&& parameterMap.containsKey("altitude")
				&& parameterMap.containsKey("longitude")
				&& parameterMap.containsKey("id_route")) {
			String stationName = req.getParameterValues("name")[0];
			float altitude = Float.parseFloat(req.getParameterValues("altitude")[0]);
			float longitude = Float.parseFloat(req.getParameterValues("longitude")[0]);
			int route_id = Integer.parseInt(req.getParameterValues("id_route")[0]);

			Station station = new Station(stationName, altitude, longitude);

			Schedule.getInstance().getRouteById(route_id).addStation(station);

			FactoryRW.insert(station, route_id);
		}//if parameterMap
		resp.sendRedirect("/pages/station_list.do");
	}//work

}//StationAddServlet
